var agularValidate = angular.module('agularValidate', []);

/**
 * 增加错误提示
 */
var addTips = function(jElement, tips){
	removeTips(jElement);
	var tipsEle = $('<div class="b-validate-tips" style="color:#AE1027;"> <i class="glyphicon glyphicon-remove" ></i> <span>'
	    + tips
	    + '</span> </div>');
	jElement.parent().append(tipsEle);
};

/**
 * 移除错误提示
 */
var removeTips = function(jElement){
	try{
		$(".b-validate-tips", jElement.parent()).remove();
	}catch(e){}
};

agularValidate.directive('maxdbColsize', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '') {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if(!commonutil.testMaxDBColumnSize(viewValue,attrs.maxdbColsize)){
        			addTips(elm,'后台数据库最大长度为：'+attrs.maxdbColsize)
                    ctrl.$setValidity('maxDBColumnSize', false);
                    return undefined;
        		} else {
        			  removeTips(elm)
                      ctrl.$setValidity('maxDBColumnSize', true);
                      return viewValue;
        		}
            });
        }
    };
});

agularValidate.directive('mindbColsize', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '') {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if(!commonutil.testMinCharLength(viewValue,attrs.mindbColsize)){
        			addTips(elm,'后台数据库最小长度为：'+attrs.mindbColsize)
                    ctrl.$setValidity('mindbColsize', false);
                    return undefined;
        		} else {
        			  removeTips(elm)
                      ctrl.$setValidity('mindbColsize', true);
                      return viewValue;
        		}
            });
        }
    };
});

agularValidate.directive('floatType', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '')  {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if( commonutil.isfloat(viewValue)){
	    			removeTips(elm)
	                ctrl.$setValidity('floatType', true);
	                return viewValue;
        		} else {
        			addTips(elm,'该字段只能是小数')
                    ctrl.$setValidity('floatType', false);
                    return undefined;
        			
        		}
            });
        }
    };
});

agularValidate.directive('intType', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '')  {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if(viewValue != '' && (!commonutil.checkRate(viewValue) ||  parseInt(viewValue)>2147483647)){
	            	addTips(elm,'该字段只能是整数，且不超过2147483647')
	                ctrl.$setValidity('intType', false);
	                return undefined;
        		} else {
        			removeTips(elm)
	                ctrl.$setValidity('intType', true);
	                return viewValue;
        			
        		}
            });
        }
    };
});

agularValidate.directive('alphabetNum', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '')  {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if(commonutil.testAlphabetAndNum(viewValue)){
        			removeTips(elm)
	                ctrl.$setValidity('alphabetNum', true);
	                return viewValue;
        		} else {
        			addTips(elm,'该字段只能是字母和数字')
	                ctrl.$setValidity('alphabetNum', false);
	                return undefined;
        			
        			
        		}
            });
        }
    };
});

agularValidate.directive('num', function() {
    return {
        require : 'ngModel',
        link : function(scope, elm, attrs, ctrl) {
        	
            ctrl.$parsers.unshift(function(viewValue) {
            	if(typeof viewValue === 'undefined' || viewValue === null || viewValue === '') {
            		removeTips(elm)
	                ctrl.$setValidity('num', true);
            		return undefined;
            	}
        		if(commonutil.testNumType(viewValue)){
        			removeTips(elm)
	                ctrl.$setValidity('num', true);
	                return viewValue;
        		} else {
        			addTips(elm,'该字段只能是数字')
	                ctrl.$setValidity('num', false);
	                return undefined;
        			
        			
        		}
            });
        }
    };
});